﻿using mars_server.csharp.DB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using mars_server.csharp.model;
using MySql.Data.MySqlClient;
using mars_server.csharp.Util;

namespace mars_server.csharp.dao.impl
{
    public class UserManagerImpl : UserManager
    {
        private UserManagerImpl() { }
        private static readonly UserManagerImpl instance = new UserManagerImpl();
        public static UserManagerImpl Instance
        {
            get
            {
                return instance;
            }
        }

        ConnectionPool pool = ConnectionPool.getInstance();//mysql连接池，单例模式

        string tableName = "mars_user";//这个manager对应的表

        /**
         * 根据主键来获取用户的信息
         * */
        public UserDO selectByPrimaryKey(long id)
        {
            MySqlConnection connection = pool.getConnection();
            string query = SqlUtil.selectByPrimaryKey(tableName, id);
            try
            {
                MySqlCommand cmd = new MySqlCommand(query, connection);
                MySqlDataReader dataReader = cmd.ExecuteReader();
                while (dataReader.Read())
                {
                    UserDO userDO = new UserDO();
                    this.dataReaderToUserDO(userDO, dataReader);
                    pool.releaseConnection(connection);
                    dataReader.Close();
                    return userDO;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());

            }
            finally
            {
                pool.releaseConnection(connection);
            }
            return null;
        }

        /**
         * 根据主键来删除用户的信息
         * */
        public bool delectByPrimaryKey(long id)
        {
            MySqlConnection connection = pool.getConnection();
            string query = SqlUtil.selectByPrimaryKey(tableName, id);
            try
            {
                MySqlCommand cmd = new MySqlCommand(query, connection);
                cmd.ExecuteNonQuery();
                pool.releaseConnection(connection);
                return true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            finally
            {
                pool.releaseConnection(connection);
            }
            return false;
        }

        /**
         * 根据主键列表来批量获取用户
         * */
        public List<UserDO> selectUserDOsByUserIds(List<long> userIds)
        {
            List<UserDO> userDOs = new List<UserDO>();
            string query = SqlUtil.selectByIds(tableName, userIds);
            MySqlConnection connection = pool.getConnection();

            try
            {
                MySqlCommand cmd = new MySqlCommand(query, connection);
                MySqlDataReader dataReader = cmd.ExecuteReader();
                while (dataReader.Read())
                {
                    UserDO userDO = new UserDO();
                    dataReaderToUserDO(userDO, dataReader);
                    userDOs.Add(userDO);
                }
                dataReader.Close();
            }
            catch(Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            finally
            {
                pool.releaseConnection(connection);
            }
            return userDOs;
        }

        /**
         * 根据singleMap 来获取UserDO LIST
         * */
        public List<UserDO> selectUserDOBySingleMap(Dictionary<string,string> sqlMap)
        {
            MySqlConnection connection = pool.getConnection();
            string filed="", value="";
            foreach(var map in sqlMap)
            {
                filed = map.Key;
                value = map.Value;
                break;
            }
            string query = SqlUtil.selectByStringQuery(tableName, filed, value);
            try
            {
                List<UserDO> userDOs = new List<UserDO>();
                MySqlCommand cmd = new MySqlCommand(query, connection);
                MySqlDataReader dataReader = cmd.ExecuteReader();
                while (dataReader.Read())
                {
                    UserDO userDO = new UserDO();
                    this.dataReaderToUserDO(userDO, dataReader);
                    userDOs.Add(userDO);
                }
                pool.releaseConnection(connection);
                dataReader.Close();
                return userDOs;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            finally
            {
                pool.releaseConnection(connection);
            }
            return null;
        }

        /**
          * 根据singleMap并通过正则表达式来获取UserDO LIST
          * */
        public List<UserDO> selectUserDOBySingleMapRegexp(Dictionary<string, string> sqlMap)
        {
            MySqlConnection connection = pool.getConnection();
            string filed = "", value = "";
            foreach (var map in sqlMap)
            {
                filed = map.Key;
                value = map.Value;
                break;
            }
            string query = SqlUtil.selectByStringQueryRegexp(tableName, filed, value);
            try
            {
                List<UserDO> userDOs = new List<UserDO>();
                MySqlCommand cmd = new MySqlCommand(query, connection);
                MySqlDataReader dataReader = cmd.ExecuteReader();
                while (dataReader.Read())
                {
                    UserDO userDO = new UserDO();
                    this.dataReaderToUserDO(userDO, dataReader);
                    userDOs.Add(userDO);
                }
                pool.releaseConnection(connection);
                dataReader.Close();
                return userDOs;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            finally
            {
                pool.releaseConnection(connection);
            }
            return null;

        }


        //dataReader 转 UserDO
        public void dataReaderToUserDO(UserDO userDO, MySqlDataReader dataReader)
        {
            userDO.id = long.Parse(dataReader[0].ToString());
            userDO.username = dataReader[1].ToString();
            userDO.password = dataReader[2].ToString();
            userDO.nickName = dataReader[3].ToString();
            userDO.sex = int.Parse(dataReader[4].ToString());
            userDO.userIntro = dataReader[5].ToString();
            userDO.headImg = dataReader[6].ToString();
        }

       

    }




    
}
